package com.summerli.service.impl;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.summerli.common.ResponseResult;
import com.summerli.common.constant.IdentityConstant;
import com.summerli.common.seriviceverificationcode.VerifyCodeResponse;
import com.summerli.service.VerificationCodeService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpMethod;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

@Service
@Slf4j
public class VerificationCodeServiceImpl implements VerificationCodeService {
    @Autowired
    private RestTemplate restTemplate;

    @Value("${verification-code-service.name}")
    private String VERIFICATION_CODE_SERVICE;
    @Override
    public String getCode(String phoneNumber) {
        String url = "http://"+VERIFICATION_CODE_SERVICE+"/verify-code/generate/"+ IdentityConstant.PASSENGER+"/"+phoneNumber;
        ResponseResult resultBoby = restTemplate.exchange(url, HttpMethod.GET, null, ResponseResult.class).getBody();
        if(resultBoby.getCode() == 1){
            VerifyCodeResponse response = JSONObject.toJavaObject((JSON) JSON.toJSON(resultBoby.getData()), VerifyCodeResponse.class);
            return response.getData();
        }else{
            return "";
        }
    }

    @Override
    public String verifyCode(String phoneNumber, String code) {

        return null;
    }
}
